package cn.edu.hunau.java.week01.day04;

import org.junit.Test;

import java.util.Arrays;
import java.util.Random;

public class RandomDataArrayTest {
    @Test

    public void test() {
        int count = 0;
        while (count < 10) {
            int[] data = getRandomDataArray(6, 33);
            System.out.println(Arrays.toString(data));
            count++;
        }
    }


    public int[] getRandomDataArray(int length, int bound){
        int [] result=new int[length];
        //声明一个标记数组标记数据是否存在
        //随机范围[1,33]那么如果数据为33,flags
        boolean[] flags=new boolean[bound+1];
        Random r=new Random();
        for(int i=0;i<length;i++){
            int temp=r.nextInt(bound)+1;
            if(flags[temp]){
                i--;
                System.out.println("出现重复");
            }else{
                result[i]=temp;
                flags[temp]=true;
            }
        }
        return result;
    }
}
